package client.comm.baoding.fragment

import android.view.View
import client.comm.baoding.R
import client.comm.baoding.adapter.ZcjlAdapter
import client.comm.baoding.api.bean.Zcjl
import client.comm.baoding.databinding.FragmentLoadmorepageBinding
import client.comm.baoding.ui.vm.ZcjlViewModel
import client.comm.commlib.base.BaseFragment
import client.comm.commlib.comm_ui.draw.SpaceItemDecoration
import client.comm.commlib.widget.EmptyLayout

class StoreIncomeOneFragment : BaseFragment<FragmentLoadmorepageBinding, ZcjlViewModel>() {
    override fun getViewId(): Int {
        return R.layout.fragment_loadmorepage
    }

    val adapter: ZcjlAdapter by lazy {
        ZcjlAdapter(requireContext(), mImgLoader)
    }

    override fun initViewModel(): ZcjlViewModel = createViewModel()

    override fun initWidget() {

        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.holdStoreIncome()
        }
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))

        binding.recyclerView.adapter = adapter
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.holdStoreIncome()
            }
        }
        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true

            viewModel.pageOne.value = 0
            viewModel.holdStoreIncome()
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.callStoreIncome.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.record_list as MutableList<Zcjl.Record?>?
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
    }

    override fun initData() {
        viewModel.pageOne.value = 0
        viewModel.holdStoreIncome()
    }


}